<style type="text/css">
    .action-cell {
        margin-bottom: 20px;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div style="min-width: 1024px">
        <h4>训练记录</h4>
        <Alert show-icon>
            此处用于训练记录管理。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="addModal=true">
                <Icon type="md-add-circle"/>
                添加
            </Button>
            <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                <Icon type="md-close-circle"/>
                删除
            </Button>
        </ButtonGroup>
        <Row>
            <Col>
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                       @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>
        <div style="margin: 10px;overflow: hidden">
            <div style="float: right;">
                <Page :total="total" size="large" show-total :current="current" show-elevator show-sizer
                      @on-change="changePage"></Page>
            </div>
        </div>
        <!--添加-->
        <Modal v-model="addModal" title="添加记录" @on-ok="handleSubmitAdd('formValidate')"
               @on-cancel="addModal =false">
            <Form ref="formValidate" :model="formValidate" :label-width="100">
                <Form-item label="记录日期">
                    <Row>
                        <i-col span="11">
                            <Date-picker type="datetime" placeholder="请输入训练记录日期"
                                         @on-change="changeStartDate"></Date-picker>
                        </i-col>
                    </Row>
                </Form-item>
                <Form-item label="训练记录内容" prop="Logcontent">
                    <i-input v-model="formValidate.Logcontent" placeholder="请输入训练记录内容"></i-input>
                </Form-item>
            </Form>
        </Modal>
        <!--编辑-->
        <Modal v-model="editModal" title="编辑" @on-ok="handleSubmitEdit('formValidateEdit')"
               @on-cancel="editModal =false">
            <Form ref="formValidateEdit" :model="formValidateEdit" :label-width="100">
                <Form-item label="选择分组" prop="videoGroup">
                    <i-select v-model="formValidateEdit.groupId" placeholder="请选择监控分组">
                        <template v-for="(item,index) in videoGroup">
                            <i-option :value="item.Id">{{item.Title}}</i-option>
                        </template>
                    </i-select>
                </Form-item>
                <Form-item label="摄像头名称" prop="title">
                    <i-input v-model="formValidateEdit.title" placeholder="请输入摄像头名称"></i-input>
                </Form-item>
                <Form-item label="IP地址" prop="ip">
                    <i-input v-model="formValidateEdit.ip" placeholder="请输入IP地址"></i-input>
                </Form-item>
                <Form-item label="用户名" prop="username">
                    <i-input v-model="formValidateEdit.username" placeholder="请输入用户名"></i-input>
                </Form-item>
                <Form-item label="密码" prop="password">
                    <i-input v-model="formValidateEdit.password" type="password" placeholder="请输入密码"></i-input>
                </Form-item>
                <Form-item label="端口" prop="port">
                    <i-input v-model="formValidateEdit.port" type="number" placeholder="请输入端口"></i-input>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                tableData: [],
                //添加
                formValidate: {
                    //添加字段
                    Logcontent: '',
                    Logdate: '',
                },
                chooseAreaArr: [],
                addModal: false,
                //编辑
                formValidateEdit: {
                    groupId: '',
                    title: '',
                    username: '',
                    password: '',
                    port: '',
                    ip: '',
                },
                editModal: false,
                //操作按钮状态管理
                deleteBtnEnable: true,
                selectedItems: [],
                //表格
                total: 0,
                current: 1,
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '记录内容',
                        key: 'Logcontent',
                        align: 'center',
                    },
                    {
                        title: '计划周期',
                        key: 'PlainType',
                        align: 'center',
                    },
                    {
                        title: '训练目标',
                        key: 'Target',
                        align: 'center',
                    },
                    {
                        title: '训练内容',
                        key: 'Targetcontent',
                        align: 'center',
                    },
                    {
                        title: '记录时间',
                        key: 'Logdate',
                        align: 'center',
                    },
                ],
                //监控分组数据
                videoGroup:'',
            }
        },
        mounted() {
            this.mockTableData();
        },
        methods: {
            //跳转
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            //选择时间
            changeStartDate: function (date) {
                console.log(date);
                this.formValidate.Logdate = date;
            },

            mockTableData() {
                let that = this;

                let para = {
                    curPage: this.current,
                    pageSize: 10,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GetTrainingLogList, para, function (res) {
                    that.$Loading.finish();
                    that.total = res.Msg.TotalCount;
                    that.current = res.Msg.CurrentPage;
                    that.tableData = res.Msg.Arrays;
                });

            },
            changePage(row) {
                // 这里直接更改了模拟的数据，真实使用场景应该从服务端获取数据
                console.log("row", row);
                this.current = row;
                this.mockTableData();
            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:',selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ')
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //删除
            remove: function () {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的记录项，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);

                        apiUtil.post(that, apiUtil.urls.DelTrainingLog, params, function (res) {
                            that.$Message.success('删除成功。');
                            that.mockTableData();
                        });
                    }
                });
            },
            //添加
            handleSubmitAdd(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                });
            },
            addData: function (model) {
                let that = this;

                model.Trainingid =that.$route.query.id;
                model.Loggerid =sessionStorage.getItem('userId');
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log(params);

                apiUtil.post(that, apiUtil.urls.AddTrainingLog, params, function (res) {
                    that.$Loading.finish();
                    that.addModal = false;
                    that.mockTableData();
                    that.$Message.info('添加成功 ')
                    that.handleReset('formValidate');

                });
            },
            //重置表单
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            //点击编辑
            editClick: function (row) {
                this.editModal =true;
                this.formValidateEdit.title =row.Title;
                this.formValidateEdit.username =row.Loginusername;
                this.formValidateEdit.password =row.Loginuserpwd;
                this.formValidateEdit.ip =row.Ip;
                this.formValidateEdit.port =row.Loginport;
                this.formValidateEdit.id =row.Id;
                this.formValidateEdit.groupId =row.Groupid;
            },
            handleSubmitEdit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPSEdit(model);
                })
            },
            comfirmModifyPSEdit: function (model) {
                let that = this;
                let params = {};

                params = that.$qs.stringify(model);
                apiUtil.post(that, apiUtil.urls.EditCamera, params, function (res) {
                    that.$Message.info('修改成功 ');
                    that.editModal =false;
                    that.mockTableData();

                });
            },
        },
    }
</script>
